import ctypes
import inspect
import json
import socket
import threading
import time

from flask import Flask, request
import socket


app = Flask(__name__)


msg_num = 0


@app.route("/received_msg", methods=['POST', 'GET'])
def get_msg():
    body = request.data
    try:
        body = request.data.decode("utf", "backslashreplace")
    except BaseException as e:
        print("body1: %s" % body)
        #logger.info("body1: %s" % body)
        try:
            body = request.body.decode("utf-8")
        except BaseException as e:
            #logger.info("body2: %s" % body)
            print("body2: %s" % body)
            pass
    try:
        rsp_json = request.body_arguments["json"][0].decode("utf-8")
    except Exception:
        rsp_json = body
    # logging.info(rsp_json)
    # print(rsp_json)
    msg = json.loads(rsp_json)
    if msg["Content"].get("RecordOutputResult"):
        global msg_num
        msg_num += 1
        print(f"msg_num : {msg_num}")
    # print(msg)
    return "hi"


if __name__ == '__main__':
    # 获取计算机名称
    hostname = socket.gethostname()
    # 获取本机IP
    system_ip = socket.gethostbyname(hostname)

    app.run(host=system_ip, port=5000, debug=True)